<?php
require_once dirname(__FILE__) . "/../config/functions.php";
$fullUrl = full_url();
include_once dirname(__FILE__) . '/header.php';

//check for delete
$deleteFlag = FALSE;
if (isset($_GET['action']) && $_GET['action'] == 'delete') {
    if (isset($_GET['id']) && $_GET['id']) {
        mysql_query("delete from ads WHERE id = " . $_GET['id']);
        $deleteFlag = TRUE;
    }
}

//add new
if (isset($_POST) && $_POST) {
    //check for errors
    $errors = array();
    if (!$_POST['text_ad']) {
        $errors[] = 'You must add title.';
    }
    if (!$_POST['url']) {
        $errors[] = 'You must add url.';
    }
    if (!$_POST['level']) {
        $errors[] = 'You must add level number.';
    }
    if (count($errors) > 0) {
        
    } else {
        $title = $_POST['text_ad'];
        $url = $_POST['url'];
        $level = $_POST['level'];
        mysql_query("insert into `ads` (`textAd`, `url`, `level`) values ('$title','$url', '$level') ");
    }
}

$itemsPerPage = 10;
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$ofsset = ($page - 1) * $itemsPerPage;
$limit = "limit $ofsset,$itemsPerPage";

$query = "SELECT * from ads where `level` IS NOT NULL AND `level` !=0";

$ads = mysql_query("$query $limit");
//count all ads
$adsCount = mysql_query("$query");
$adsCount = mysql_num_rows($adsCount);

//calculate the last page number
$lastPageNumber = (int) ($adsCount / $itemsPerPage);
if (($adsCount % $itemsPerPage) > 0) {
    $lastPageNumber++;
}
?>
<div id="page-wrapper">

    <div class="row">
        <div class="col-lg-12">
            <h1>Admin Settings <small>Update Urls</small></h1>

            <?php if ($deleteFlag): ?>
                <div class="alert alert-success">
                    Record Deleted Successfully.
                </div>
            <?php endif; ?>

            <a href="javascript:void(0)" onclick="$('.addNewAd').slideToggle();" class="addNewAdBtn btn btn-default">Add New</a>

            <?php
            //add new
            if (isset($_POST) && $_POST) {
                if (count($errors) > 0) {
                    foreach ($errors as $error) {
                        echo '<div class="alert alert-danger">' . $error . '</div>';
                    }
                } else {
                    echo '<div class="alert alert-success">Record Added Successfully.</div>';
                }
            }
            ?>

            <div class="addNewAd">
                <form class="form-horizontal" role="form" method="post">
                    <div class="form-group">
                        <label for="text_ad" class="col-sm-2 control-label">Text Ad</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="text_ad" id="text_ad">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="url" class="col-sm-2 control-label">Url</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="url" id="url">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="level" class="col-sm-2 control-label">level</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" name="level" id="level">
                            <label style="font-weight: normal; font-size: 14px;">Ex. 1, 2, 3, ...</label>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-primary">Add</button>
                        </div>
                    </div>
                </form>
            </div>

            <table class="table table-bordered">
                <thead><tr><th>#</th><th>Text Ad</th><th>Url</th><th>Level</th><th>Action</th></tr></thead>
                <tbody>
                    <?php
                    $index = 1;
                    while ($row = mysql_fetch_array($ads)) :
                        ?>
                        <tr>
                            <td><?php echo $index; ?></td>
                            <td><?php echo $row['textAd']; ?></td>
                            <td><?php echo $row['url']; ?></td>
                            <td><?php echo ($row['level'] == 0)?'Sponsor Ad':$row['level']; ?></td>
                            <td>
                                <a href="urls.php?action=delete&id=<?php echo $row['id']; ?>" onclick="return confirm('Are you sure you want to remove this record.');"><span class="glyphicon glyphicon-remove"></span></a>
                            </td>
                        </tr>
                        <?php
                        $index++;
                    endwhile;
                    ?>
                </tbody>
            </table>

            <?php
            if ($lastPageNumber > 1):
                $fullUrl = strtok($fullUrl, '?');
                ?>
                <ul class="pagination">
                    <?php if ($page > 1): ?>
                        <li>
                            <a href="<?php
                            $params = $_GET;
                            $params['page'] = $page - 1;
                            $paramString = http_build_query($params);
                            echo $fullUrl . '?' . $paramString;
                            ?>">«</a>
                        </li>
                    <?php endif; ?>
                    <?php for ($index = 1; $index <= $lastPageNumber; $index++): ?>
                        <?php if ($index > $page - 3 && $index < $page + 3): ?>
                            <li <?php if ($index == $page): ?>class="active"<?php endif; ?>>
                                <a href="<?php
                                $params = $_GET;
                                $params['page'] = $index;
                                $paramString = http_build_query($params);
                                echo $fullUrl . '?' . $paramString;
                                ?>"><?php echo $index; ?></a>
                            </li>
                        <?php endif; ?>
                    <?php endfor; ?>    
                    <?php if ($page < $lastPageNumber): ?>
                        <li>
                            <a href="<?php
                            $params = $_GET;
                            $params['page'] = $page + 1;
                            $paramString = http_build_query($params);
                            echo $fullUrl . '?' . $paramString;
                            ?>">»</a>
                        </li>
                    <?php endif; ?>
                </ul>
            <?php endif; ?>
        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->
<?php include_once dirname(__FILE__) . '/footer.php'; ?>

